package logic

import (
	"context"
	"gorm.io/gorm"
	"vipshop/vipshoprpc/models/mysql"

	"vipshop/vipshoprpc/internal/svc"
	"vipshop/vipshoprpc/vipshoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic {
	return &UpdatePasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatePasswordLogic) UpdatePassword(in *vipshoprpc.UpdatePasswordRequest) (*vipshoprpc.UpdatePasswordResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("id=?", in.Id).Find(&user)

	users := mysql.User{
		Model:    gorm.Model{ID: uint(in.Id)},
		UserName: in.Username,

		Password: in.Password,
	}
	mysql.DB.Debug().Updates(&users)
	return &vipshoprpc.UpdatePasswordResponse{
		Id: int64(users.ID),
	}, nil
}
